﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bezeq_online_work.CONTROL
{
   public class BusinessLogic
    {
       model.DB_workSet modelconection = new model.DB_workSet();
       private int acc_attempts = 0;
       private DateTime test ;
       private bool IsTiming = false;
       private static int Usertype=-1;
       private string userNum="";
       private int blockss;

        public int authentication(string username, string password)
        {
            try
            {
                if (can_try_login())
                {
                    List<string> col = new List<string>();
                    List<string> con = new List<string>();
                    List<string> userid = new List<string>();
                    col.Add("SystemID");
                    con.Add("UserName = '" + username + "'");
                    con.Add("PassWord = '" + password + "'");
                    if ((userid = modelconection.getWorker(col, con)).Count > 0)
                    {
                        userNum = userid[0].Substring(2);
                        Usertype = 1;
                        return 1;
                    }
                    else if ((userid = modelconection.getTeamManager(col, con)).Count > 0)
                    {
                        userNum = userid[0].Substring(2);
                        Usertype = 2;
                        return 2;
                    }
                    else if ((userid = modelconection.getManager(col, con)).Count > 0)
                    {
                        userNum = userid[0].Substring(2);
                        Usertype = 3;
                        return 3;
                    }
                    else
                    { return 0; }
                }
                else
                { return -1; }
            }
            catch (Exception ex)
            {
                // throw error message
                throw new Exception(ex.Message);
            }
          }
               
        private bool can_try_login()
        {
            if (!IsTiming && acc_attempts < 6)
        {
          acc_attempts++;
          return true ;
         } 
        if (IsTiming )
        {
          if (test + TimeSpan.FromSeconds(300) < DateTime.Now)
          {
            acc_attempts = 0;
            IsTiming=false;
            return true;
          }
          else
          {
              return false;
          }
        }
        else 
        {
            IsTiming = true ; 
            test=DateTime.Now;
            return false;
        }

        }

        public bool isWorker()
        { return Usertype == 1; } 

        public string workerBlocksCount()
        {
            try
            {

                List<string> col = new List<string>();
                List<string> con = new List<string>();
                col.Add("TeamWork");
                con.Add("SystemID='" + userNum + "'");

                string team = (modelconection.getWorker(col, con)[0]).Substring(2);
                col.Clear();
                con.Clear();
                col.Add("TeamAtMoked");
                con.Add("SystemID='" + team + "'");
                string moked = modelconection.getTeamManager(col, con)[0].Substring(2);
                col.Clear();
                con.Clear();
                col.Add("NumOfBlocks");
                con.Add("Moked_ID='" + moked + "'");
                string blocks = modelconection.getSettings(col, con)[0].Substring(2);
                blockss = Convert.ToInt32(blocks);
                return blocks;
            }
            catch (Exception ex)
            {
                // throw error message
                throw new Exception(ex.Message);
            }
        }

        public int BlocksCountInt()
        {
            try
            {
                workerBlocksCount();
                return blockss;
            }
            catch (Exception ex)
            {
                // throw error message
                throw new Exception(ex.Message);
            }
        }

        public void addBlockRequest(bool[] array, string info)
        { 
              try
                {
                 
                List<string> col = new List<string>();
                List<string> con = new List<string>();
                  col.Add("WorkerID");
                  con.Add("WorkerID='" + userNum + "'");
                  if (modelconection.getBlockRequest(col, con).Count > 0)
                  {
                      modelconection.deleteBlockRequest(con);
                  }
                  col.Clear();
                  col.Add("DEFAULT");
                  col.Add(userNum);
               DateTime   nextSunday = DateTime.Today.AddDays(7 - (int) DateTime.Today.DayOfWeek);
                  col.Add(nextSunday.AddDays(7).ToShortDateString());
                  col.Add(nextSunday.AddDays(13).ToShortDateString());
                  col.Add(DateTime.Now.ToShortDateString());
                
                  for (int i = 0; i < 14; i++)
                  {
                      if (array[i] )
                      { col.Add("'TRUE'"); }
                      else
                      {
                          col.Add("'FALSE'");
                      }
                  }
                
                      col.Add("'"+info+"'");
                    
                  modelconection.insertBlockRequest(col);
                /*  col = new List<string>();
                  con = new List<string>();
                  col.Add("*");
                  con.Add("WorkerID='" + userNum + "'");
                 List<string> d =  modelconection.getBlockRequest(col,con);*/
                 }
         catch (Exception ex)
            {
                // throw error message
                throw new Exception(ex.Message);
            }
        }

        public List<string>[] retriveWorkArregnmanet(string fromDate)
        {
            try{
            List<string>[] shifts = new List<string>[14];
            List<string> col = new List<string>();
            List<string> con = new List<string>();
               col.Add("TeamWork");
                con.Add("SystemID='" + userNum + "'");

                string team = (modelconection.getWorker(col, con)[0]).Substring(2);
                col.Clear();
                con.Clear();
                col.Add("TeamAtMoked");
                con.Add("SystemID='" + team + "'");
                string moked = modelconection.getTeamManager(col, con)[0].Substring(2);
             col.Clear();
             con.Clear();
             string sup = "";
             List<string> temp;

             for (int i = 0; i < 14;i++ )
             {
                 col.Clear();
                 con.Clear();
              col.Add("WorkerID");
              con.Add("ShiftSetFrom = '" + fromDate + "'");//'2014-02-09T00:00:00.000' format
           
             con.Add("s"+((i/2)%8+1).ToString()+(i%2+1).ToString()+" ='True'");
            temp = modelconection.getWorkerShiftsSet(col,con);
            shifts[i] = new List<string>();
            foreach (string t in temp)
            {
                col.Clear();
                con.Clear();

                col.Add("TeamWork");
                con.Add("SystemID='" + t.Substring(2) + "'");
                sup = modelconection.getWorker(col, con)[0].Substring(2);
                col.Clear();
                con.Clear();
                col.Add("TeamAtMoked");
                con.Add("SystemID='" + sup + "'");
                sup = modelconection.getTeamManager(col, con)[0].Substring(2);
                if (moked.CompareTo(sup) == 0)
                {
                    col.Clear();
                    con.Clear();
                    col.Add("FirstName");
                    con.Add("SystemID='" + t.Substring(2) + "'");
                    sup = modelconection.getWorker(col, con)[0].Substring(2);
                    col.Clear();
                    col.Add("LastName");
                    shifts[i].Add(sup + modelconection.getWorker(col, con)[0].Substring(2));
                }
            
               }
             }
             return shifts;
                  }
         catch (Exception ex)
            {
                // throw error message
                throw new Exception(ex.Message);
            }
        }

        public List<string>[] retriveWorkArregnmanetThis()
        {
            DateTime d = DateTime.Today;
            int offset = d.DayOfWeek - DayOfWeek.Sunday;
            DateTime lastsunday = d.AddDays(-offset);
            //string fromDate = lastsunday.ToUniversalTime().ToString();
            string fromDate = lastsunday.ToString("yyyy-MM-ddTHH:mm:ss.fff");//2014-02-09T00:00:00.000
             return retriveWorkArregnmanet(fromDate);
        }

        /* public string tester()
        { List<string> names = new List<string>() ;
        List<string> names2 = new List<string>();
            //names.Add("33");
           names.Add("234343");
            names.Add("'dfdfsfdd'"); 
            //names.Add("'fsdfsdfs'");
            //names.Add("04/04/2004");
            names.Add("'fuck this shit'");
        
            names2.Add("WorkerID");
            names2.Add("WorkerLastName");
            names2.Add("ExtraInformation");
           // names.Add("*");
           // names2.Add("SystemID > '0'");
         // names =   modelconection.GET(1, names, names2) ;
            

           modelconection.INPUT(1,names2,names);
           
           names = new List<string>();
           names2 = new List<string>();
           names.Add("*");
           names2.Add("SystemID > '4'");
          modelconection.DELETE(1, names2);
           names2 = new List<string>();
           names2.Add("SystemID > '0'");
           names = modelconection.GET(1, names, names2);
          return names[names.Count -1];
        }*/

    }
}
